<?xml version='1.0' encoding='UTF-8' ?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml"
	xmlns:ui="http://java.sun.com/jsf/facelets"
	xmlns:h="http://java.sun.com/jsf/html"
	xmlns:f="http://java.sun.com/jsf/core"
	xmlns:p="http://primefaces.org/ui"
	xmlns:sec="http://www.springframework.org/security/facelets/tags">
	
<h:body>
	<ui:composition template="/templates/templateCRUD.xhtml">
		<ui:param name="tituloCadastro" value="Cadastro de Clínicas" />
		<ui:param name="tituloPesquisa" value="Pesquisa de Clínicas" />
		<ui:param name="tituloGrid" value="Lista de Empresa" />
		<ui:param name="mbean" value="${empresaMB}" />
		<ui:define name="cadastro">
			<p:tabView id="abaCadastro">
				<p:tab title="Dados da empresa">
				    <p:focus for="razaoSocialCadastro"/>
					<h:panelGrid columns="3">
						<h:outputText value="Razao Social (*):" />
						<p:inputText value="#{mbean.bean.razaoSocial}" disabled="#{mbean.readOnly}" size="54" maxlength="50" required="true" label="Razao Social" id = "razaoSocialCadastro"/>
						<p:message for="razaoSocialCadastro" showDetail="true" showSummary="true" />
						
						<h:outputText value="Fantasia (*):" />
						<p:inputText value="#{mbean.bean.nomeFantasia}" disabled="#{mbean.readOnly}" size="54" maxlength="50" required="true" label="Fantasia" id="fantasiaCadastro"/>
						<p:message for="fantasiaCadastro" showDetail="true" showSummary="true" />
						
						<h:outputText value="CNPJ (*):" />
						<p:inputMask value="#{mbean.bean.cnpj}" disabled="#{mbean.readOnly}" size="54" maxlength="50" required="true" label="CNPJ"
							mask="99.999.999/9999-99" id="cnpjCadastro">
							<f:validator validatorId="CNPJValidator"/>
						</p:inputMask>
						<p:message for="cnpjCadastro" showDetail="true" showSummary="true" />
						
						<h:outputText value="Inscricao Estadual:" />
						<p:inputText value="#{mbean.bean.inscricaoEstadual}" disabled="#{mbean.readOnly}" size="54" maxlength="50" required="false" label="IE" id="ieCadastro"/>
						<p:message for="ieCadastro" showDetail="true" showSummary="true" />
					    
					</h:panelGrid>
				</p:tab>
				
				<p:tab title="Endereço">
					<h:panelGrid columns="3">
						<h:outputText value="Endereco:" />
						<p:inputText value="#{mbean.bean.endereco.logradouro}" disabled="#{mbean.readOnly}" size="54" 
							maxlength="50" required="false" label="Endereco" id="enderecoCadastro"/>
						<p:message for="enderecoCadastro" showDetail="true" showSummary="true" />
						
						<h:outputText value="Número:" />
						<p:inputText value="#{mbean.bean.endereco.numero}" disabled="#{mbean.readOnly}" size="20" 
							maxlength="20" required="false" label="Número" id="numeroCadastro"/>
						<p:message for="numeroCadastro" showDetail="true" showSummary="true" />
						
						<h:outputText value="Complemento:" />
						<p:inputText value="#{mbean.bean.endereco.complemento}" disabled="#{mbean.readOnly}" size="54" 
							maxlength="50" required="false" label="Complemento" id="complementoCadastro"/>
						<p:message for="complementoCadastro" showDetail="true" showSummary="true" />
							
						<h:outputText value="Bairro:" />
						<p:inputText value="#{mbean.bean.endereco.bairro}" disabled="#{mbean.readOnly}" size="54" 
							maxlength="50" required="false" label="Bairro" id="bairroCadastro"/>
						<p:message for="bairroCadastro" showDetail="true" showSummary="true" />
						
						<h:outputText value="Cidade:" />
						<p:inputText value="#{mbean.bean.endereco.cidade}" disabled="#{mbean.readOnly}" size="54" 
							maxlength="50" required="false" label="Cidade" id="cidadeCadastro"/>
						<p:message for="cidadeCadastro" showDetail="true" showSummary="true" />
						
						<h:outputText value="Estado:" />
						<p:selectOneMenu value="#{mbean.bean.endereco.UF}" disabled="#{mbean.readOnly}" label="UF" required="false" effect="drop" 
							id="estadoCadastro">
							<f:selectItems value="#{mbean.listaEstados}"/>
						</p:selectOneMenu>
						<p:message for="estadoCadastro" showDetail="true" showSummary="true" />
							
						<h:outputText value="CEP:" />
						<p:inputMask value="#{mbean.bean.endereco.logradouro}" disabled="#{mbean.readOnly}" size="20" 
							maxlength="20" required="false" label="CEP" mask="99999-999" id="cepCadastro"/>
						<p:message for="cepCadastro" showDetail="true" showSummary="true" />
					</h:panelGrid>
				</p:tab>
				
				<p:tab title="Telefone">
					<p:panel header="Novo">
						<h:panelGrid columns="5">
							<h:outputText value="Tipo (*):" />
							<p:selectOneMenu value="#{mbean.telefone.tipo}"
							                 disabled="#{mbean.readOnly}" 
							                 label="Tipo" required="false" 
							                 effect="drop"
								             id="tipoTelefone">
								<f:selectItems value="#{mbean.tiposTelefone}"  var="tipo" itemLabel="#{tipo.nome}"/>
							</p:selectOneMenu>
	
							<h:outputText value="Número (*):" />
							<p:inputMask value="#{mbean.telefone.numero}" disabled="#{mbean.readOnly}" size="30" 
								maxlength="50" required="false" label="Número" mask="(99) 9999-9999" id="numeroTelefone"/>
							
							<p:commandButton value="Adicionar" actionListener="#{mbean.adicionarTelefone}" update="gridTelefones tipoTelefone numeroTelefone messageNumeroTelefone" 
								disabled="#{mbean.readOnly}" process="@this tipoTelefone numeroTelefone"/>
						</h:panelGrid>
						<br/>
						<p:message for="numeroTelefone" showDetail="true" showSummary="false" id="messageNumeroTelefone"/>
						
					</p:panel>
					
					<p:panel header="Cadastrados">
						<p:dataTable var="registro" value="#{mbean.bean.telefones}" emptyMessage="" id="gridTelefones">
							<p:column>
								<f:facet name="header">
									<h:outputText value="Tipo"/>
								</f:facet>
								<h:outputText value="#{registro.tipo}"/>
							</p:column>
							<p:column>
								<f:facet name="header">
									<h:outputText value="Número"/>
								</f:facet>
								<h:outputText value="#{registro.numero}"/>
							</p:column>
				          	<p:column style="width:32px">  
					             <p:commandButton icon="ui-icon-trash" action="#{mbean.excluirTelefone(registro)}" 
					             	disabled="#{mbean.readOnly}" immediate="true" oncomplete="atualizarGrid();"/>
							</p:column> 
						</p:dataTable>
					</p:panel>
					
					<p:remoteCommand update="gridTelefones" name="atualizarGrid"/>
				</p:tab>	
			</p:tabView>
		</ui:define>

		<ui:define name="pesquisa">
		    <p:focus for="razaoPesquisa"/>
			<h:panelGrid columns="2">
				<h:outputText value="Razão Social:" />
				<p:inputText value="#{mbean.beanSearch.razaoSocial}" size="54" maxlength="50" id="razaoPesquisa"/>
				<h:outputText value="CNPJ:" />
				<p:inputMask value="#{mbean.beanSearch.cnpj}" size="54" maxlength="50"
							mask="99.999.999/9999-99"/>
			</h:panelGrid>
		</ui:define>
		
		<ui:define name="gridPesquisa">
			<p:column>
				<f:facet name="header">
					<h:outputText value="Razão Social"/>
				</f:facet>
				<h:outputText value="#{registro.razaoSocial}"/>
			</p:column>
			
			<p:column>
				<f:facet name="header">
					<h:outputText value="CNPJ"/>
				</f:facet>
				<h:outputText value="#{registro.cnpj}"/>
			</p:column>
		</ui:define>
	</ui:composition>
</h:body>
</html>
